package cn.edu.kzjsxy.graduationproject.service.impl;

import cn.edu.kzjsxy.graduationproject.common.Page;
import cn.edu.kzjsxy.graduationproject.common.Result;
import cn.edu.kzjsxy.graduationproject.common.TokenUtil;
import cn.edu.kzjsxy.graduationproject.entity.Buildings;
import cn.edu.kzjsxy.graduationproject.entity.Users;
import cn.edu.kzjsxy.graduationproject.mapper.BuildingsMapper;
import cn.edu.kzjsxy.graduationproject.service.IBuildingsService;
import cn.edu.kzjsxy.graduationproject.service.IUsersService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import javax.servlet.http.HttpServletRequest;
import java.util.*;

/**
 * <p>
 * 楼栋信息表 服务实现类
 * </p>
 *
 * @author chén jiā jùn
 * @since 2024-04-14
 */
@Service
public class BuildingsServiceImpl extends ServiceImpl<BuildingsMapper, Buildings> implements IBuildingsService {

//	楼栋管理的mapper
	@Autowired
	BuildingsMapper buildingsMapper;

//	用户信息的service
	@Autowired
	private IUsersService usersService;

	//	tokenUitl
	@Autowired
	private TokenUtil tokenUtil;

//    <!-- 根据条件查询楼栋信息 -->
	@Override
	public List<Buildings> selectBuildingsByCondition(Buildings buildings, HttpServletRequest request) {
		Users user = tokenUtil.resolveToken(request);
		if(Objects.equals(user.getRole(), "0")){
			buildings.setHousemasterId(user.getUserId());
		}
		List<Buildings> buildingsList = buildingsMapper.selectBuildingsByCondition(buildings);
		buildingsList = getHousemasterNameByHousemasterId(buildingsList);
		return buildingsList;
	}


//	分页根据条件查询 楼栋信息
	@Override
	public Result<PageInfo<Buildings>> selectBuildingsByPage(Page page, Buildings building,HttpServletRequest request) {
		Users user = tokenUtil.resolveToken(request);
		if(Objects.equals(user.getRole(), "0")){
			building.setHousemasterId(user.getUserId());
		}
		// 设置分页
		PageHelper.startPage(page.getPageNum(), page.getPageSize());
		// 执行查询
		List<Buildings> buildingsList = buildingsMapper.selectBuildingsByCondition(building);
		buildingsList = getHousemasterNameByHousemasterId(buildingsList);
		PageInfo<Buildings> pageInfo = new PageInfo<>(buildingsList);
		return Result.success(pageInfo);
	}

	//	根据宿管id查询宿管姓名 housemasterName
	@Override
	public Buildings getHousemasterNameByHousemasterId(Buildings buildings) {
		if(buildings!=null){
			List<String> housemasterIds = parseBuildingIdToBuildingList(buildings.getHousemasterId());
			String housemasterName = getHousemasterNameById(housemasterIds);
			buildings.setHousemasterName(housemasterName);
		}
		return buildings;
	}

//	根据宿管id查询宿管姓名 housemasterName
	@Override
	public List<Buildings> getHousemasterNameByHousemasterId(List<Buildings> buildingsList) {
		if(buildingsList!=null&&!buildingsList.isEmpty()){
			for (Buildings building : buildingsList) {
				List<String> housemasterIds = parseBuildingIdToBuildingList(building.getHousemasterId());
				String housemasterName = getHousemasterNameById(housemasterIds);
				building.setHousemasterName(housemasterName);
			}
		}
		return buildingsList;
	}


//	把宿管id解析成为List集合
public List<String> parseBuildingIdToBuildingList(String housemasterId) {
	if (StringUtils.hasText(housemasterId)) {
		return Arrays.asList(housemasterId.split(","));
	} else {
		return Collections.emptyList(); // 返回一个空列表
	}
}


//	根据宿管id查询宿管姓名
public String getHousemasterNameById(List<String> housemasterIds) {
	StringJoiner housemasterName = new StringJoiner(", ");
	if (housemasterIds != null && !housemasterIds.isEmpty()) {
		for (String housemasterId : housemasterIds) {
			Users users = usersService.getById(housemasterId);
			if (users != null) {
				housemasterName.add(users.getName());
			}
		}
	}
	return housemasterName.toString();
}
}




